﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

/**
 * 创建时间：2014/11/26 14:50:46
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.Common
{
    public class HMACSHAHelper
    {
        public string SignHMACSHA1(string content, bool isBase64 = true)
        {
            string result = string.Empty;
            try
            {
                byte[] secretkey = new Byte[64];
                secretkey = Encoding.UTF8.GetBytes(KEYS.KEY_SHA_SECRETKEY);
                using (HMACSHA1 provider = new HMACSHA1(secretkey))
                {
                    byte[] hashedPassword = provider.ComputeHash(Encoding.UTF8.GetBytes(content));
                    StringBuilder displayString = new StringBuilder();
                    for (int counter = 0; counter < hashedPassword.Length; counter++)
                    {
                        displayString.Append(hashedPassword[counter].ToString("x2"));
                    }
                    result = displayString.ToString();
                }
                //LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
                if (isBase64 && !string.IsNullOrWhiteSpace(result))
                {
                    result = Convert.ToBase64String(Encoding.UTF8.GetBytes(result));
                }
                //LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
            }

            return result;
        }
    }
}
